package com.librarysys.service.impl;

import com.librarysys.common.Result;
import com.librarysys.dao.BookcaseMapper;
import com.librarysys.dao.BookinfoMapper;
import com.librarysys.dao.BooktypeMapper;
import com.librarysys.dao.PublishingMapper;
import com.librarysys.entity.Bookcase;
import com.librarysys.entity.Bookinfo;
import com.librarysys.entity.Booktype;
import com.librarysys.entity.Publishing;
import com.librarysys.service.IBookService;
import com.librarysys.vo.BookDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yuhui on 2017/7/12.
 */
@Service("iBookService")
public class BookServiceImpl implements IBookService {


    @Autowired
    private BookinfoMapper bookinfoMapper;

    @Autowired
    private BookcaseMapper bookcaseMapper;

    @Autowired
    private PublishingMapper publishingMapper;

    @Autowired
    private BooktypeMapper booktypeMapper;

    public Result listBookType(){
        List<Booktype> booktypes = booktypeMapper.selectAll();
        return Result.createBySuccess("查询成功",booktypes);
    }

    public Result addBookType(Booktype booktype){
        int insert = booktypeMapper.insert(booktype);
        if(insert>0){
            return Result.createBySuccess("添加成功");
        }else {
            return Result.createByError("添加失败");
        }
    }

    public Result delBookType(Integer id){
        int i = booktypeMapper.deleteByPrimaryKey(id);
        if(i>0){
            return Result.createBySuccess("删除成功");
        }else {
            return Result.createByError("删除失败");
        }
    }

    public Result listBook(){
        List<Bookinfo> bookinfos = bookinfoMapper.selectAll();
        List<BookDetailVO> list = toBookDetailVOs(bookinfos);
        return Result.createBySuccess("查询成功",list);
    }

    private List<BookDetailVO> toBookDetailVOs(List<Bookinfo> bookinfos) {
        List<BookDetailVO> list=new ArrayList<BookDetailVO>();
        for(Bookinfo bookinfo:bookinfos){
            BookDetailVO bookDetailVO = new BookDetailVO();
            bookDetailVO.setId(bookinfo.getId());
            bookDetailVO.setAuthor(bookinfo.getAuthor());
            bookDetailVO.setBarcode(bookinfo.getBarcode());
            bookDetailVO.setBookName(bookinfo.getBookname());
            Bookcase bookcase = bookcaseMapper.selectByPrimaryKey(bookinfo.getBookcase());
            if(bookcase==null){
                bookDetailVO.setBookcase("");
            }else{
                bookDetailVO.setBookcase(bookcase.getName());
            }
            Publishing publishing = publishingMapper.selectBy(bookinfo.getIsbn());
            if(publishing==null){
                bookDetailVO.setPublishing("");
            }else{
                bookDetailVO.setPublishing(publishing.getPubname());
            }
            Booktype booktype = booktypeMapper.selectByPrimaryKey(bookinfo.getTypeid());
            if(booktype==null){
                bookDetailVO.setTypeNmae("");
            }else{
                bookDetailVO.setTypeNmae(booktype.getTypename());
            }

            list.add(bookDetailVO);
        }
        return list;
    }

    public Result delBook(Integer id){
        int i = bookinfoMapper.deleteByPrimaryKey(id);
        if(i>0){
            return Result.createBySuccess("删除成功");
        }else {
            return Result.createByError("删除失败");
        }
    }

    public Result doQueryBook(BookDetailVO bookDetailVO){
        if(bookDetailVO!=null){
            Integer bookcaseId=null;
            if(bookDetailVO.getBookcase()!=null){
                //查询书架id
                Bookcase bookcase = bookcaseMapper.selectByName(bookDetailVO.getBookcase());
                bookcaseId=bookcase.getId();
            }
            String publishIds="";
            if(bookDetailVO.getPublishing()!=null){
                //查询出版社
                List<Publishing> publishings = publishingMapper.selectByName("%" + bookDetailVO.getPublishing() + "%");
                for(Publishing publishing:publishings){
                    publishIds=publishIds+publishing.getIsbn()+",";
                }
                publishIds=publishIds.substring(0,publishIds.length()-1);
                System.out.println(publishIds);
            }
            String ids="";
            if(bookDetailVO.getTypeNmae()!=null){
                List<Booktype> booktypes = booktypeMapper.selectByName("%" + bookDetailVO.getTypeNmae() + "%");
                for(Booktype booktype:booktypes){
                    ids=ids+booktype.getId()+",";
                }
                ids=ids.substring(0,ids.length()-1);
                System.out.println(ids);
            }
            String bookname=null;
            if(bookDetailVO.getBookName()!=null){
                bookname="%"+bookDetailVO.getBookName()+"%";
            }
            List<Bookinfo> bookinfos = bookinfoMapper.selectByQuery(bookDetailVO.getBarcode(), ids, publishIds,bookname , bookcaseId);
            List<BookDetailVO> bookDetailVOS = toBookDetailVOs(bookinfos);
            return Result.createBySuccess("查询成功",bookDetailVOS);
        }else{
            return Result.createByError("参数错误");
        }
    }
}
